import requests
from typing import Dict, List, Union
from .base import BaseModel

class ClaudeModel(BaseModel):
    """Claude模型实现"""
    
    def __init__(self, api_key: str, model_name: str, api_url: str):
        super().__init__(api_key, model_name)
        self.api_url = api_url
        self.supports_thinking = True
    
    def generate_response(self, messages: List[Dict[str, str]], enable_thinking: bool = False) -> str:
        headers = {
            "x-api-key": self.api_key,
            "Content-Type": "application/json",
            "anthropic-version": "2023-06-01"
        }
        
        # 提取系统消息
        system_message = ""
        claude_messages = []
        
        for msg in messages:
            if msg["role"] == "system":
                system_message = msg["content"]
            else:
                claude_messages.append(msg)
        
        data = {
            "model": self.model_name,
            "max_tokens": 1000,
            "system": system_message,
            "messages": claude_messages
        }
        
        response = requests.post(self.api_url, headers=headers, json=data, timeout=30)
        response.raise_for_status()
        
        result = response.json()
        return result['content'][0]['text'].strip()